# Script to help run benchmarks!  Uses ab.
# M

def app_installed?(app)
  w = `whereis #{app}`
  !w[File::Separator].nil?
end

# Make sure these specify a path, not just a domain (i.e. don't forget trailing slash)
hosts = {'local' => 'localhost:3000/',
         'local_artwork' => 'localhost:3000/catalogue/show/10-sculpture',
         'local_artist' => 'localhost:3000/artists/4-john-doe',
         'local_region' => 'localhost:3000/regions/1',
         'ea' => 'http://cubist.cs.washington.edu:3125/',
         }
requests = 1000
concurrency_levels = [1, 2, 4]

unless app_installed?('ab')
  $stderr.puts "You need ab installed to run benchmarks, silly."
  exit
end

time = 10
puts "All .txt, .csv, and .gnuplot files in the current directory will be erased in #{time} seconds.  Terminate to abort."
for i in 1..time
  print '.'
  $stdout.flush
  sleep 1
end
puts "\nDeleted"
`rm -f *.txt *.csv *.gnuplot`

hosts.each do |host_name, host_url|
  concurrency_levels.each do |conc|
    filenameish = "#{host_name.to_s}_benchmark_n#{requests}_c#{conc}"
    puts "Benchmarking #{host_url} with #{requests} total requests, #{conc} at a time."
    %x{ab -n #{requests} -c #{conc} -e #{filenameish}.csv -g #{filenameish}.gnuplot #{host_url} > #{filenameish}.txt}
  end
end